#include "ActionBleedThroughEvalOnFile.hpp"
#include <QImage>
#include "ImageConverter.hpp"
#include <DoQuMeasures/BleedThroughTrinarisation.hpp>
#include <QImageWriter>



QString ActionBleedThroughEvalOnFile::oldRectoPath;


ActionBleedThroughEvalOnFile::ActionBleedThroughEvalOnFile(MesureTemplateMethod &pEval, const QString &versoPath)
    : _versoPath(versoPath),
    eval(pEval)
{}

ActionBleedThroughEvalOnFile::~ActionBleedThroughEvalOnFile()
{

}

float ActionBleedThroughEvalOnFile::doAction(const QFileInfo &f)
{
    QImage qorigin(f.absoluteFilePath());
    QImage qverso(_versoPath);
    std::cout << qverso.width() << std::endl;
    std::cout << qorigin.width() << std::endl;

    qverso = qverso.mirrored(true,false);

    ImageConverter ctrl;
    const  girl::tools::Image original = ctrl.convert(qorigin);
    const girl::tools::Image verso= ctrl.convert(qverso);


    BleedThroughTrinarisation * tri = NULL;
    if(oldRectoPath != f.absoluteFilePath())
    {

        tri = BleedThroughTrinarisation::reset(original,verso);
        oldRectoPath = f.absoluteFilePath();
    }
    else
        tri = BleedThroughTrinarisation::instance();

    girl::tools::Image trinarized = tri->trinarized();

    QImage trin = ctrl.convert(trinarized);
    QImageWriter writer1("Trinarized.png", "png");
    writer1.write(trin);



    return eval.evaluate(original);
}
